
#include <vector>
#include <algorithm>
#include "raylib.h"
#include <hero.h>
#include <thread>
using namespace std;

vector<float> Hero::getCollisionBox()
{
    vector<float> collisionBox;
    collisionBox.push_back(x+6);
    collisionBox.push_back(y);
    collisionBox.push_back(x+width+4);
    collisionBox.push_back(y+height);

    return collisionBox;
}

Rectangle Hero::getRectangle()
{
    Rectangle temp={x+6,y,width-2,height};
    return temp;
}

Texture Hero::initTexture(string imgPath){
    // 使用c_str()正确获取C风格字符串
    Image character = LoadImage(imgPath.c_str());
    ImageResize(&character, 120, 160);
    Texture chara_texture = LoadTextureFromImage(character);
    // SetTextureFilter(chara_texture,TEXTURE_FILTER_POINT);
    UnloadImage(character);
    return chara_texture;
}

void Hero::setInitialPosition(float x,float y)
{
    Vector2 temp={x,y};
    this->position=temp;
}

Vector2 Hero::getPosition()
{
    return {x,y};
}

//缩放大小的时候一定要等比例同步改这四个50的变量
void Hero::changeDirection(int direction)
{
    vector<Rectangle> frames;
    frames.reserve(3); // 预留空间提高效率
    
    // 根据方向计算Y坐标（不同方向对应不同行）
    int directions[4]={3,2,0,1};
    float yPos = directions[direction] * 40;
    
    for (int i = 0; i < 3; i++)
    {
        frames.push_back({
            (float)(i * 40),  // X坐标
            yPos,            // Y坐标（根据方向变化）
            40.0f,        // 宽度
            40.0f       // 高度
        });
    }
    actionFrames=frames;
}


void Hero::hero_thread(int id)
{
    vector<int> activeDirections;
    bool moving = false;
    while(this->isLive)
    {
         // 检测按键按下，添加到数组,当遍历到end时即代表没找到
        if(IsKeyDown(KEY_W) && 
           find(activeDirections.begin(), activeDirections.end(), 0) == activeDirections.end()) {
            activeDirections.push_back(0);
            moving=true;
        }
        if(IsKeyDown(KEY_D) && 
           find(activeDirections.begin(), activeDirections.end(), 1) == activeDirections.end()) {
            activeDirections.push_back(1);
            moving=true;
        }
        if(IsKeyDown(KEY_S) && 
           find(activeDirections.begin(), activeDirections.end(), 2) == activeDirections.end()) {
            activeDirections.push_back(2);
            moving=true;
        }
        if(IsKeyDown(KEY_A) && 
           find(activeDirections.begin(), activeDirections.end(), 3) == activeDirections.end()) {
            activeDirections.push_back(3);
            moving=true;
        }
        
        // 检测按键释放，从数组中移除，
        if(IsKeyUp(KEY_W)) {
            auto it = find(activeDirections.begin(), activeDirections.end(), 0);
            if (it != activeDirections.end()) {
                activeDirections.erase(it);
            }
        }
        if(IsKeyUp(KEY_D)) {
            auto it = find(activeDirections.begin(), activeDirections.end(), 1);
            if (it != activeDirections.end()) {
                activeDirections.erase(it);
            }
        }
        if(IsKeyUp(KEY_S)) {
            auto it = find(activeDirections.begin(), activeDirections.end(), 2);
            if (it != activeDirections.end()) {
                activeDirections.erase(it);
            }
        }
        if(IsKeyUp(KEY_A)) {
            auto it = find(activeDirections.begin(), activeDirections.end(), 3);
            if (it != activeDirections.end()) {
                activeDirections.erase(it);
            }
        }
        
        // 如果有按下的方向键，使用最后一个作为基准方向并移动
        if (!activeDirections.empty()) {
            int lastDirection = activeDirections.back();  // 获取最后按下的方向
            this->direction = lastDirection;  // 更新全局方向
            
            // 根据最后按下的方向移动
            switch (lastDirection) {
                case 0: // W - 上
                    run_up(step);
                    break;
                case 1: // D - 右
                    run_right(step);
                    break;
                case 2: // S - 下
                    run_down(step);
                    break;
                case 3: // A - 左
                    run_left(step);
                    break;
            }
        }
        if(IsKeyUp(KEY_A)&&IsKeyUp(KEY_D)&&IsKeyUp(KEY_S)&&IsKeyUp(KEY_W))
            moving =false;
        this_thread::sleep_for(chrono::milliseconds(10));
    
    }
}